package cn.zn.modules.appoin.controller;

import cn.zn.framework.common.utils.R;
import cn.zn.modules.appoin.entity.AtrWeekEntity;
import cn.zn.modules.appoin.entity.AtrWeekWorkTimeEntity;
import cn.zn.modules.appoin.service.AtrWeekService;
import cn.zn.modules.appoin.service.AtrWeekWorkTimeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 规则：总周
 * @author zn
 * @email zhunahao@qq.com
 * @date 2020-09-01 14:15:28
 */
@RestController
@RequestMapping("appoin/atrweek")
public class AtrWeekController {

    @Autowired
    AtrWeekService atrWeekService;
    @Autowired
    AtrWeekWorkTimeService atrWeekWorkTimeService;

    /**
     * 列表
     */
    @GetMapping("/list/{ruleId}")
    public R list(@PathVariable("ruleId") String ruleId) {

        return R.ok().put("list", atrWeekService.list(new QueryWrapper<AtrWeekEntity>().eq("RULE_ID_", ruleId).orderByAsc("SORT_NUM_")));
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {

        AtrWeekEntity atrWeekEntity = atrWeekService.getById(id);
        List<AtrWeekWorkTimeEntity> weekWorkTimes = atrWeekWorkTimeService.list(new QueryWrapper<AtrWeekWorkTimeEntity>()
                .eq("WEEK_ID_", id)
                .orderByAsc("START_TIME_"));
        atrWeekEntity.setWeekWorkTimes(weekWorkTimes);
        return R.ok().put("week", atrWeekEntity);
    }

    /**
     * 更新
     * @param atrWeekEntity
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody AtrWeekEntity atrWeekEntity) {
        atrWeekService.updateWeek(atrWeekEntity);
        return R.ok();
    }
}
